//Page Object
import { request } from "../../request/request";
Page({
  data: {
    swiperData: [],
    navData: [],
    floorData: [],
  },
  //options(Object)
  onLoad: function (options) {
    // 获取轮播图数据
    this.getSwiperData();
    // 获取导航数据
    this.getNavData();
    //  获取楼层数据
    this.getFloorData();
  },
  // 获取轮播图数据
  getSwiperData() {
    request({
      url: "/home/swiperdata",
    }).then((res) => {
      // console.log(res);
      let swiperData = res.message;
      // 把后台接口中的main替换成goods_detail
      swiperData = swiperData.map((item) => {
        item.navigator_url = item.navigator_url.replace("main", "goods_detail");
        return item;
      });
      // console.log(swiperData);
      this.setData({
        swiperData,
      });
    });
  },
  // 获取导航数据
  getNavData() {
    request({
      url: "/home/catitems",
    }).then((res) => {
      // console.log(res);
      this.setData({
        navData: res.message,
      });
    });
  },
  // 获取楼层数据
  getFloorData() {
    request({
      url: "/home/floordata",
    }).then((res) => {
      // console.log(res);
      let floorData = res.message;
      // 把后台接口中的链接地址中添加goods_list
      floorData = floorData.map((item1) => {
        item1.product_list.map((item2) => {
         let index = item2.navigator_url.search(/[?]/g)
         item2.navigator_url = item2.navigator_url.slice(0, index) + "/goods_list" + item2.navigator_url.slice(index)
        //  console.log(item2);
        });
        return item1
      });
      // console.log(floorData);
      this.setData({
        floorData
      });
    });
  },
});
